home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / email / _parseaddr.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  11KB  |  474 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __all__ = [
  5.     'mktime_tz',
  6.     'parsedate',
  7.     'parsedate_tz',
  8.     'quote']
  9. import time
  10. SPACE = ' '
  11. EMPTYSTRING = ''
  12. COMMASPACE = ', '
  13. _monthnames = [
  14.     'jan',
  15.     'feb',
  16.     'mar',
  17.     'apr',
  18.     'may',
  19.     'jun',
  20.     'jul',
  21.     'aug',
  22.     'sep',
  23.     'oct',
  24.     'nov',
  25.     'dec',
  26.     'january',
  27.     'february',
  28.     'march',
  29.     'april',
  30.     'may',
  31.     'june',
  32.     'july',
  33.     'august',
  34.     'september',
  35.     'october',
  36.     'november',
  37.     'december']
  38. _daynames = [
  39.     'mon',
  40.     'tue',
  41.     'wed',
  42.     'thu',
  43.     'fri',
  44.     'sat',
  45.     'sun']
  46. _timezones = {
  47.     'UT': 0,
  48.     'UTC': 0,
  49.     'GMT': 0,
  50.     'Z': 0,
  51.     'AST': -400,
  52.     'ADT': -300,
  53.     'EST': -500,
  54.     'EDT': -400,
  55.     'CST': -600,
  56.     'CDT': -500,
  57.     'MST': -700,
  58.     'MDT': -600,
  59.     'PST': -800,
  60.     'PDT': -700 }
  61.  
  62. def parsedate_tz(data):
  63.     data = data.split()
  64.     if data[0].endswith(',') or data[0].lower() in _daynames:
  65.         del data[0]
  66.     else:
  67.         i = data[0].rfind(',')
  68.         if i >= 0:
  69.             data[0] = data[0][i + 1:]
  70.         
  71.     if len(data) == 3:
  72.         stuff = data[0].split('-')
  73.         if len(stuff) == 3:
  74.             data = stuff + data[1:]
  75.         
  76.     
  77.     if len(data) == 4:
  78.         s = data[3]
  79.         i = s.find('+')
  80.         if i > 0:
  81.             data[3:] = [
  82.                 s[:i],
  83.                 s[i + 1:]]
  84.         else:
  85.             data.append('')
  86.     
  87.     if len(data) < 5:
  88.         return None
  89.     
  90.     data = data[:5]
  91.     (dd, mm, yy, tm, tz) = data
  92.     mm = mm.lower()
  93.     if mm not in _monthnames:
  94.         dd = mm
  95.         mm = dd.lower()
  96.         if mm not in _monthnames:
  97.             return None
  98.         
  99.     
  100.     mm = _monthnames.index(mm) + 1
  101.     if mm > 12:
  102.         mm -= 12
  103.     
  104.     if dd[-1] == ',':
  105.         dd = dd[:-1]
  106.     
  107.     i = yy.find(':')
  108.     if i > 0:
  109.         yy = tm
  110.         tm = yy
  111.     
  112.     if yy[-1] == ',':
  113.         yy = yy[:-1]
  114.     
  115.     if not yy[0].isdigit():
  116.         yy = tz
  117.         tz = yy
  118.     
  119.     if tm[-1] == ',':
  120.         tm = tm[:-1]
  121.     
  122.     tm = tm.split(':')
  123.     if len(tm) == 2:
  124.         (thh, tmm) = tm
  125.         tss = '0'
  126.     elif len(tm) == 3:
  127.         (thh, tmm, tss) = tm
  128.     else:
  129.         return None
  130.     
  131.     try:
  132.         yy = int(yy)
  133.         dd = int(dd)
  134.         thh = int(thh)
  135.         tmm = int(tmm)
  136.         tss = int(tss)
  137.     except ValueError:
  138.         return None
  139.  
  140.     tzoffset = None
  141.     tz = tz.upper()
  142.     if _timezones.has_key(tz):
  143.         tzoffset = _timezones[tz]
  144.     else:
  145.         
  146.         try:
  147.             tzoffset = int(tz)
  148.         except ValueError:
  149.             pass
  150.  
  151.     if tzoffset:
  152.         if tzoffset < 0:
  153.             tzsign = -1
  154.             tzoffset = -tzoffset
  155.         else:
  156.             tzsign = 1
  157.         tzoffset = tzsign * ((tzoffset // 100) * 3600 + (tzoffset % 100) * 60)
  158.     
  159.     return (yy, mm, dd, thh, tmm, tss, 0, 1, -1, tzoffset)
  160.  
  161.  
  162. def parsedate(data):
  163.     t = parsedate_tz(data)
  164.     if isinstance(t, tuple):
  165.         return t[:9]
  166.     else:
  167.         return t
  168.  
  169.  
  170. def mktime_tz(data):
  171.     if data[9] is None:
  172.         return time.mktime(data[:8] + (-1,))
  173.     else:
  174.         t = time.mktime(data[:8] + (0,))
  175.         return t - data[9] - time.timezone
  176.  
  177.  
  178. def quote(str):
  179.     return str.replace('\\', '\\\\').replace('"', '\\"')
  180.  
  181.  
  182. class AddrlistClass:
  183.     
  184.     def __init__(self, field):
  185.         self.specials = '()<>@,:;."[]'
  186.         self.pos = 0
  187.         self.LWS = ' \t'
  188.         self.CR = '\r\n'
  189.         self.FWS = self.LWS + self.CR
  190.         self.atomends = self.specials + self.LWS + self.CR
  191.         self.phraseends = self.atomends.replace('.', '')
  192.         self.field = field
  193.         self.commentlist = []
  194.  
  195.     
  196.     def gotonext(self):
  197.         while self.pos < len(self.field):
  198.             if self.field[self.pos] in self.LWS + '\n\r':
  199.                 self.pos += 1
  200.                 continue
  201.             self
  202.             if self.field[self.pos] == '(':
  203.                 self.commentlist.append(self.getcomment())
  204.                 continue
  205.             break
  206.  
  207.     
  208.     def getaddrlist(self):
  209.         result = []
  210.         while self.pos < len(self.field):
  211.             ad = self.getaddress()
  212.             if ad:
  213.                 result += ad
  214.                 continue
  215.             result.append(('', ''))
  216.         return result
  217.  
  218.     
  219.     def getaddress(self):
  220.         self.commentlist = []
  221.         self.gotonext()
  222.         oldpos = self.pos
  223.         oldcl = self.commentlist
  224.         plist = self.getphraselist()
  225.         self.gotonext()
  226.         returnlist = []
  227.         if self.pos >= len(self.field):
  228.             if plist:
  229.                 returnlist = [
  230.                     (SPACE.join(self.commentlist), plist[0])]
  231.             
  232.         elif self.field[self.pos] in '.@':
  233.             self.pos = oldpos
  234.             self.commentlist = oldcl
  235.             addrspec = self.getaddrspec()
  236.             returnlist = [
  237.                 (SPACE.join(self.commentlist), addrspec)]
  238.         elif self.field[self.pos] == ':':
  239.             returnlist = []
  240.             fieldlen = len(self.field)
  241.             self.pos += 1
  242.             while self.pos < len(self.field):
  243.                 self.gotonext()
  244.                 returnlist = returnlist + self.getaddress()
  245.                 continue
  246.                 None if self.pos < fieldlen and self.field[self.pos] == ';' else self
  247.         elif self.field[self.pos] == '<':
  248.             routeaddr = self.getrouteaddr()
  249.             if self.commentlist:
  250.                 returnlist = [
  251.                     (SPACE.join(plist) + ' (' + ' '.join(self.commentlist) + ')', routeaddr)]
  252.             else:
  253.                 returnlist = [
  254.                     (SPACE.join(plist), routeaddr)]
  255.         elif plist:
  256.             returnlist = [
  257.                 (SPACE.join(self.commentlist), plist[0])]
  258.         elif self.field[self.pos] in self.specials:
  259.             self.pos += 1
  260.         
  261.         self.gotonext()
  262.         if self.pos < len(self.field) and self.field[self.pos] == ',':
  263.             self.pos += 1
  264.         
  265.         return returnlist
  266.  
  267.     
  268.     def getrouteaddr(self):
  269.         if self.field[self.pos] != '<':
  270.             return None
  271.         
  272.         expectroute = False
  273.         self.pos += 1
  274.         self.gotonext()
  275.         adlist = ''
  276.         while self.pos < len(self.field):
  277.             if expectroute:
  278.                 self.getdomain()
  279.                 expectroute = False
  280.             elif self.field[self.pos] == '>':
  281.                 self.pos += 1
  282.                 break
  283.             elif self.field[self.pos] == '@':
  284.                 self.pos += 1
  285.                 expectroute = True
  286.             elif self.field[self.pos] == ':':
  287.                 self.pos += 1
  288.             else:
  289.                 adlist = self.getaddrspec()
  290.                 self.pos += 1
  291.                 break
  292.             self.gotonext()
  293.             continue
  294.             self
  295.         return adlist
  296.  
  297.     
  298.     def getaddrspec(self):
  299.         aslist = []
  300.         self.gotonext()
  301.         while self.pos < len(self.field):
  302.             if self.field[self.pos] == '.':
  303.                 aslist.append('.')
  304.                 self.pos += 1
  305.             elif self.field[self.pos] == '"':
  306.                 aslist.append('"%s"' % self.getquote())
  307.             elif self.field[self.pos] in self.atomends:
  308.                 break
  309.             else:
  310.                 aslist.append(self.getatom())
  311.             self.gotonext()
  312.         if self.pos >= len(self.field) or self.field[self.pos] != '@':
  313.             return EMPTYSTRING.join(aslist)
  314.         
  315.         aslist.append('@')
  316.         self.pos += 1
  317.         self.gotonext()
  318.         return EMPTYSTRING.join(aslist) + self.getdomain()
  319.  
  320.     
  321.     def getdomain(self):
  322.         sdlist = []
  323.         while self.pos < len(self.field):
  324.             if self.field[self.pos] in self.LWS:
  325.                 self.pos += 1
  326.                 continue
  327.             self
  328.             if self.field[self.pos] == '(':
  329.                 self.commentlist.append(self.getcomment())
  330.                 continue
  331.             None if self.field[self.pos] == '[' else self
  332.             if self.field[self.pos] in self.atomends:
  333.                 break
  334.                 continue
  335.             sdlist.append(self.getatom())
  336.         return EMPTYSTRING.join(sdlist)
  337.  
  338.     
  339.     def getdelimited(self, beginchar, endchars, allowcomments = True):
  340.         if self.field[self.pos] != beginchar:
  341.             return ''
  342.         
  343.         slist = [
  344.             '']
  345.         quote = False
  346.         self.pos += 1
  347.         while self.pos < len(self.field):
  348.             if quote:
  349.                 slist.append(self.field[self.pos])
  350.                 quote = False
  351.             elif self.field[self.pos] in endchars:
  352.                 self.pos += 1
  353.                 break
  354.             elif allowcomments and self.field[self.pos] == '(':
  355.                 slist.append(self.getcomment())
  356.                 continue
  357.             elif self.field[self.pos] == '\\':
  358.                 quote = True
  359.             else:
  360.                 slist.append(self.field[self.pos])
  361.             self.pos += 1
  362.             continue
  363.             self
  364.         return EMPTYSTRING.join(slist)
  365.  
  366.     
  367.     def getquote(self):
  368.         return self.getdelimited('"', '"\r', False)
  369.  
  370.     
  371.     def getcomment(self):
  372.         return self.getdelimited('(', ')\r', True)
  373.  
  374.     
  375.     def getdomainliteral(self):
  376.         return '[%s]' % self.getdelimited('[', ']\r', False)
  377.  
  378.     
  379.     def getatom(self, atomends = None):
  380.         atomlist = [
  381.             '']
  382.         if atomends is None:
  383.             atomends = self.atomends
  384.         
  385.         while self.pos < len(self.field):
  386.             if self.field[self.pos] in atomends:
  387.                 break
  388.             else:
  389.                 atomlist.append(self.field[self.pos])
  390.             self.pos += 1
  391.             continue
  392.             self
  393.         return EMPTYSTRING.join(atomlist)
  394.  
  395.     
  396.     def getphraselist(self):
  397.         plist = []
  398.         while self.pos < len(self.field):
  399.             if self.field[self.pos] in self.FWS:
  400.                 self.pos += 1
  401.                 continue
  402.             self
  403.             if self.field[self.pos] == '"':
  404.                 plist.append(self.getquote())
  405.                 continue
  406.             if self.field[self.pos] == '(':
  407.                 self.commentlist.append(self.getcomment())
  408.                 continue
  409.             if self.field[self.pos] in self.phraseends:
  410.                 break
  411.                 continue
  412.             plist.append(self.getatom(self.phraseends))
  413.         return plist
  414.  
  415.  
  416.  
  417. class AddressList(AddrlistClass):
  418.     
  419.     def __init__(self, field):
  420.         AddrlistClass.__init__(self, field)
  421.         if field:
  422.             self.addresslist = self.getaddrlist()
  423.         else:
  424.             self.addresslist = []
  425.  
  426.     
  427.     def __len__(self):
  428.         return len(self.addresslist)
  429.  
  430.     
  431.     def __add__(self, other):
  432.         newaddr = AddressList(None)
  433.         newaddr.addresslist = self.addresslist[:]
  434.         for x in other.addresslist:
  435.             if x not in self.addresslist:
  436.                 newaddr.addresslist.append(x)
  437.                 continue
  438.         
  439.         return newaddr
  440.  
  441.     
  442.     def __iadd__(self, other):
  443.         for x in other.addresslist:
  444.             if x not in self.addresslist:
  445.                 self.addresslist.append(x)
  446.                 continue
  447.         
  448.         return self
  449.  
  450.     
  451.     def __sub__(self, other):
  452.         newaddr = AddressList(None)
  453.         for x in self.addresslist:
  454.             if x not in other.addresslist:
  455.                 newaddr.addresslist.append(x)
  456.                 continue
  457.         
  458.         return newaddr
  459.  
  460.     
  461.     def __isub__(self, other):
  462.         for x in other.addresslist:
  463.             if x in self.addresslist:
  464.                 self.addresslist.remove(x)
  465.                 continue
  466.         
  467.         return self
  468.  
  469.     
  470.     def __getitem__(self, index):
  471.         return self.addresslist[index]
  472.  
  473.  
  474.